package com.vanke.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vanke.house.mapper.SaleHouseMapper;
import com.vanke.house.model.SaleHouse;
import com.vanke.house.service.FTQQService;
import com.vanke.house.service.SaleHouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/6/20 6:29 下午
 */
@Slf4j
@Service
public class SaleHouseServiceImpl extends ServiceImpl<SaleHouseMapper, SaleHouse> implements SaleHouseService{

    @Autowired
    private FTQQService ftqqService;

    @Override
    public void add(SaleHouse saleHouse) {
        QueryWrapper<SaleHouse> wrapper = new QueryWrapper<>();
        wrapper.eq("link", saleHouse.getLink());
        Integer result = baseMapper.selectCount(wrapper);
        if (result == 0) {
            save(saleHouse);
            ftqqService.sendMessage(saleHouse);
            log.info("insert data in mysql!");
        } else {
            log.warn("house data is exist in mysql!");
        }
    }

    @Override
    public List<SaleHouse> querySaleHouseList(int page, int pageSize) {
        QueryWrapper<SaleHouse> wrapper = new QueryWrapper<>();
        String sql = "";
        if (page >= 1) {
            String limitStart = String.valueOf((page - 1) * pageSize);
            String limitOver = String.valueOf(page * pageSize);
            sql = "limit " + limitStart + ", " + limitOver;
        }
        log.info(sql);
        wrapper.last(sql);
        return baseMapper.selectList(wrapper);
    }
}
